/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.util.kyro;

import net.rubyeye.xmemcached.transcoders.CachedData;
import net.rubyeye.xmemcached.transcoders.Transcoder;

/**
 * the Transcoder for Xmemcached use Kryo to do Serializes
 * 
 * @author lcj
 * 
 */
public class KryoTranscoder implements Transcoder<Object> {

	public static final int maxSize = 1024 * 1024;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.spy.memcached.Transcoder#decode(net.spy.memcached.CachedData)
	 */
	public final Object decode(CachedData d) {
		byte[] data = d.getData();
		try {
			return KryoUitl.readObject(data);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.spy.memcached.Transcoder#encode(java.lang.Object)
	 */
	public final CachedData encode(Object o) {
		byte[] bytes = KryoUitl.writeObject(o);
		return new CachedData(-1, bytes, maxSize, -1);
	}

	public boolean isPackZeros() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isPrimitiveAsString() {
		// TODO Auto-generated method stub
		return false;
	}

	public void setCompressionThreshold(int arg0) {
		// TODO Auto-generated method stub

	}

	public void setPackZeros(boolean arg0) {
		// TODO Auto-generated method stub

	}

	public void setPrimitiveAsString(boolean arg0) {
		// TODO Auto-generated method stub

	}
}
